/**
 * 
 */
package com.epam.training.service.car;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.epam.training.domain.Car;
import com.epam.training.repository.CarRepository;

/**
 * @author Bogdan
 *
 */
@Service
public class CarServiceImpl implements CarService {
	private static final int PAGE_SIZE = 10;

	@Autowired
	private CarRepository repository;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.epam.training.service.car.CarService#getAllCar()
	 */
	@Override
	public List<Car> getAllCar() {
		return repository.findAll();
	}

	@Override
	public Page<Car> getAllCar(Integer pageNumber) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, PAGE_SIZE);
		return repository.findAll(pageRequest);
	}

	@Transactional
	@Override
	public void save(Car car) {
		repository.save(car);
	}

	@Override
	public Car getById(Integer id) {
		return repository.findOne(id);
	}

	@Override
	public void delete(Integer id) {
		repository.delete(id);
	}

	@Override
	public void update(Car car, Integer carId) {
		Car carD = repository.findOne(carId);
		
		carD.setColor(car.getColor());
		carD.setConsumption(car.getConsumption());
		carD.setEngineVolume(car.getEngineVolume());
		carD.setGearbox(car.getGearbox());
		carD.setModel(car.getModel());
		carD.setPower(car.getPower());
		carD.setPrice(car.getPrice());
		carD.setTankCapacity(car.getTankCapacity());
		carD.setYear(car.getYear());
		
		repository.save(carD);
	}

}
